package secretaryGUI;

import flexibility.userData;
import guiCode.secretaryScreenCode;

import java.awt.Color;
import java.awt.Point;
import java.sql.SQLException;

import javax.swing.table.JTableHeader;

import secretary.defineSemester;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

public class handleLowerTableEvents {
	secretaryScreenCode ssc;
	secretaryAlerts sa = new secretaryAlerts();
	private userData classData = new userData();
	private int classID;
	private int studentID;
	updateUpperTableEntries UUTE = new updateUpperTableEntries();
	updateLowerTableEntries ULTE = new updateLowerTableEntries();
	defineSemester ds = new defineSemester();
	public void click(secretaryScreenCode code, createSecretaryWindow csw,
			Point point, ClientConsole secretaryConnection,
			CachedRowSetImpl crs, int semester) {
		// semester is the relevant semester we are working on
		ssc = code;
		csw.row = csw.table.rowAtPoint(point);
		csw.col = csw.table.columnAtPoint(point);
		
		if(ssc.getEditMode()==12){
			//this is the remove course from teacher mode
			//show the teacher courses
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.GREEN);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.RED);
			classData.deleteAll();
			classData.addData(406);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
			classData.deleteAll();
			classData.addData(415);
			classData.addData(1);
			classData.addData(ssc.getTeacherID());
			classData.addData(ds.identify(ssc.getSemesterForEdit(), secretaryConnection, crs));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);
			
		}
		
		
		if(ssc.getEditMode()==11){
			//this is to add courses to teacher
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.GREEN);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.RED);
			//this will show the teacher courses
			classData.deleteAll();
			classData.addData(407);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
		}
		
		if(ssc.getEditMode()==8){
			//this will show the student's courses (manual assign)
			JTableHeader header = csw.table.getTableHeader();
			header.setBackground(Color.GREEN);
			JTableHeader header1 = csw.table_1.getTableHeader();
			header1.setBackground(Color.RED);
			classData.deleteAll();
			classData.addData(252);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
		}
		
		if(ssc.getEditMode()==7){
			//this will delete course from chose class
			classData.deleteAll();
			classData.addData(249);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 1));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			//this will delete students that were assigne to course automatically
			classData.deleteAll();
			classData.addData(250);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 1));
			classData.addData(csw.table.getValueAt(0,1));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			//this will update the table
			classData.deleteAll();
			classData.addData(248);
			classData.addData(1);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
			ssc.viewAssignedClasses();
		}
		if(ssc.getEditMode()==5){
			/*this will bring the student back to the class*/
			classData.deleteAll();
			classData.addData(90);
			classData.addData(0);
			semester = Integer.parseInt(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 4).toString());
			classID = Integer.parseInt(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 3).toString());
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 3));
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 4));
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			//this will add the student to the class in students table
			classData.deleteAll();
			classData.addData(167);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 3));
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 4));
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			/*this will delete the student from the lower table*/
			classData.deleteAll();
			classData.addData(94);
			classData.addData(0);
			classData.addData(csw.table_1.getValueAt(csw.table_1.rowAtPoint(point), 0));
			secretaryConnection.acceptMsg(classData);
			secretaryConnection.getResults();
			/*this will update the displays*/
			classData.deleteAll();
			classData.addData(87);
			classData.addData(1);
			classData.addData(semester);
			classData.addData(classID);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			UUTE.update(csw, crs);
			classData.deleteAll();
			classData.addData(93);
			classData.addData(1);
			secretaryConnection.acceptMsg(classData);
			crs = secretaryConnection.getResults();
			ULTE.update(csw, crs);
		}
		if (ssc.getEditMode()==3) {
			// this is for edit class mode
			if (csw.col == 1) {
				csw.alertsLbl.setText("This field is not editable!");
				csw.alertsLbl.setForeground(Color.RED);
			}
			if (csw.col == 2) {
				csw.alertsLbl.setForeground(Color.BLUE);
				csw.alertsLbl
						.setText("Click on classID field to add/remove students from class. Click on className to change the class name");
				sa.showMessages(csw, ssc, csw.secretaryScreen, "", "", 5);
			}
			if (csw.col == 0) {
				csw.alertsLbl.setText("Click on 'Add students to class' to add more students to this class. Click on 'Remove student from class' to remove students from this class");
				ssc.setEditMode(2);
				csw.btnAddStudentTo.setEnabled(true);
				csw.btnRemoveStudentFrom.setEnabled(true);
				csw.alertsLbl.setForeground(Color.BLUE);
				
				classData.deleteAll();
				classID= Integer.parseInt((csw.table.getValueAt(csw.row, csw.col).toString()));//this will get the classID
				if (semester == 1) {// editing next semester
					classData.addData(15);
					classData.addData(1);// this is for read
					secretaryConnection.acceptMsg(classData);
					crs = secretaryConnection.getResults();
					try {
						crs.next();
						classData.deleteAll();
						classData.addData(87);
						classData.addData(1);// this is for read
						classData.addData(crs.getString(1));
						classData.addData(classID);
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
					if (semester == 0) {// editing current semester
						classData.addData(82);
						classData.addData(1);// this is for read
						secretaryConnection.acceptMsg(classData);
						crs = secretaryConnection.getResults();
						try {
							crs.next();
							classData.deleteAll();
							classData.addData(87);
							classData.addData(1);// this is for read
							classData.addData(crs.getString(1));
							classData.addData(classID);
							secretaryConnection.acceptMsg(classData);
							crs = secretaryConnection.getResults();
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}
					try {
						if(crs.next()==false){//check if class has already students in it
							csw.btnRemoveStudentFrom.setEnabled(false);
							crs.beforeFirst();
						}
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					ULTE.update(csw, crs);

				}
			}
		}
}
